package com.bishe.serivce.Impl;

import com.bishe.dao.UserDao;
import com.bishe.pojo.LoginModel;
import com.bishe.pojo.User;
import com.bishe.serivce.IUserService;
import com.bishe.utils.EcoBootException;
import com.bishe.utils.JobUtil;
import com.bishe.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户接口实现
 * @date 2022/2/28 13:51
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    @Override
    public LoginModel login(LoginModel loginModel) {
        //根据用户名和密码查询
        User user = userDao.checkUser(loginModel.getUserName(), loginModel.getPassWord());
        //不存在
        if (user == null){
            throw new EcoBootException("用户名密码错误！");
        }
        //登录数据保存
        String id = user.getId();
        String token = JobUtil.login("user", id);

        //返回数据
        LoginModel loginModel1 = new LoginModel();

        if ("admin".equals(loginModel.getUserName())){
            loginModel1.setType("admin");
        }else {
            loginModel1.setType("user");
        }

        loginModel1.setUserName(user.getUserName());
        loginModel1.setToken(token);
        return loginModel1;
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    @Override
    public void updateById(User user) {
        userDao.updateById(user);
    }

    @Override
    public void save(User user) {
        userDao.save(user);
    }
}
